//
//  HosptialViewController.m
//  HealthA段
//
//  Created by MrJXT on 15/6/8.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HosptialViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "DisMess.h"
#import "DisMessCollectionViewCell.h"
#import "DiseaseMessageViewController.h"
#import "FindDiseaseViewController.h"
#import "InspectViewController.h"
#import "OperationViewController.h"
#import "DisTableViewController.h"
#import "Operation.h"
#import "OperationCell.h"
#import "Singleton.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
@interface HosptialViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>
//疾病信息
@property(nonatomic,retain)UICollectionView *disMessView;
//疾病查找
@property(nonatomic,retain)UICollectionView *disFinder;
//身体部位数组
@property(nonatomic,retain)NSMutableArray *placeArr;
//手术科室数组
@property(nonatomic,retain)NSMutableArray *operaArr;
//手术项目试图
@property(nonatomic,retain)UITableView *OperaView;
@end

@implementation HosptialViewController
- (void)dealloc
{
    [_operaArr release];
    [_OperaView release];
    [_placeArr release];
    [_disMessView release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.placeArr = [NSMutableArray array];
        self.operaArr = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"检查项目";
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [[Singleton shareSingleton] hiddenNO];
    
    [self creatDisMessView];
    [self PleaseDepartmentData];
    [self creatSegView];
    [self creatOperaView];
    [self PleaseOperaData];
    [self.view bringSubviewToFront:self.disMessView];
    [self.view sendSubviewToBack:self.OperaView];
    UIImageView *picView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    UIImage *iamge = [UIImage imageNamed:@"xiangmujiancha.jpg"];
    picView.image = iamge;
    [self.disMessView setBackgroundView:picView];
    
}
//创建segmentcontrol
-(void)creatSegView
{
    NSArray *segArr = [NSArray arrayWithObjects:@"检查项目",@"手术项目", nil];
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:segArr];
    seg.frame = CGRectMake(0, 0, WIDTH, 40);
    [self.view addSubview:seg];
    seg.backgroundColor = [UIColor whiteColor];
    [seg setTintColor:[UIColor purpleColor]];
    seg.selectedSegmentIndex = 0;
    [seg addTarget:self action:@selector(clickSeg:) forControlEvents:UIControlEventValueChanged];
    [seg release];
}
//seg点击方法
-(void)clickSeg:(UISegmentedControl *)seg
{
    NSInteger index = seg.selectedSegmentIndex;
    switch (index) {
        case 0:
            [self.view bringSubviewToFront:self.disMessView];
            self.navigationItem.title = @"检查项目";
            break;
            case 1:
            [self.view bringSubviewToFront:self.OperaView];
            self.navigationItem.title = @"手术项目";
            break;
        default:
            break;
    }
}

//创建疾病信息视图
-(void)creatDisMessView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumInteritemSpacing = 10;
    flow.minimumLineSpacing = 10;
    flow.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    flow.itemSize = CGSizeMake(0.35 *WIDTH, 40);
    
    self.disMessView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT - 49 -64 - 40) collectionViewLayout:flow];
    self.disMessView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.disMessView];
    self.disMessView.delegate = self;
    self.disMessView.dataSource = self;
    [_disMessView release];
    //注册cell
    [self.disMessView registerClass:[DisMessCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}
//每个分区的元素个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.placeArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    DisMessCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    DisMess *dm = self.placeArr[indexPath.row];
    cell.nameLabe.text = dm.name;
    return cell;
}
//请求检查项目数据
-(void)PleaseDepartmentData
{
    NSString *url = @"http://api.yi18.net/check/checkclass";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"yi18"];
        for (NSMutableDictionary *tep in arr) {
            DisMess *dm = [[DisMess alloc]init];
            [dm setValuesForKeysWithDictionary:tep];
            [self.placeArr addObject:dm];
            [dm release];
        }
        if (self.placeArr.count > 0) {
            [[Singleton shareSingleton] hiddenYes];
        }
        [self.disMessView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"请求失败");
    }];
}
//跳转疾病信息列表，传一个就诊科室id
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DisTableViewController *dbvc = [[DisTableViewController alloc]init];
    DisMess *dm = self.placeArr[indexPath.row];
    dbvc.did = dm.id;
    dbvc.title = dm.name;
    [self.navigationController pushViewController:dbvc animated:YES];
    [dbvc release];
    NSLog(@"pid===%d",dm.id);
}


//创建手术项目视图
-(void)creatOperaView
{
    UIImageView *picView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT)];
    UIImage *image = [UIImage imageNamed:@"jjjjj.jpg"];
    picView.image = image;
    
    self.OperaView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT - 64 - 49 - 40) style:UITableViewStylePlain];
    [self.view addSubview:self.OperaView];
    [self.OperaView setBackgroundView:picView];
    self.OperaView.backgroundColor = [UIColor clearColor];
    self.OperaView.delegate = self;
    self.OperaView.dataSource = self;
    [_OperaView release];
    self.OperaView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}
#pragma mark 实现协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.operaArr count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    OperationCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[OperationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    Operation *op = self.operaArr[indexPath.row];
    cell.nameLabel.text = op.name;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
//tableview透明
//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
////    UIColor *altCellColor = [UIColor colorWithWhite:1.0 alpha:0.0];
////    
////    cell.backgroundColor = altCellColor;
////    altCellColor = [UIColor colorWithWhite:1.0 alpha:0.0];
////    cell.textLabel.backgroundColor = altCellColor;
////    cell.detailTextLabel.backgroundColor = altCellColor;
//    cell.backgroundColor = [UIColor clearColor];
//}
//解析身体部位列表数据
-(void)PleaseOperaData
{
    NSString *url = @"http://api.yi18.net/surgery/place";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"yi18"];
        for (NSMutableDictionary *temp in arr) {
            Operation *opt = [[Operation alloc]init];
            [opt setValuesForKeysWithDictionary:temp];
            [self.operaArr addObject:opt];
            [opt release];
        }
        [self.operaArr removeLastObject];
        NSLog(@"数组长度=%ld",self.operaArr.count);
        [self.OperaView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"手术科室数据请求失败");
    }];
}
//跳转手术信息列表
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OperationViewController *ovc = [[OperationViewController alloc]init];
    Operation *op = self.operaArr[indexPath.row];
    ovc.did = op.id;
//    NSLog(@"did==%ld",op.id);
    ovc.title = op.name;
    [self.navigationController pushViewController:ovc animated:YES];
    [ovc release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
